import os
import django
import json

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "medQuest.settings.dev")
django.setup()

from django.contrib.auth.models import User
from questionnaire.models import Questionnaire, Question, Answer

def main(filename):
    with open(filename, encoding='utf8') as f:
        data = json.load(f)

    questionnaire = Questionnaire.objects.create(
        title='多元统计分析调查',
        description='多元统计项目式教学-配合中医检测调查问卷',
        creator=User.objects.get(id=1)
    )

    for idx, question in enumerate(data):
        q = Question.objects.create(
            questionnaire=questionnaire,
            qnum=idx+1,
            title=question['question_text'],
            qtype=question['question_type'],
            options=json.dumps(question['options']),
            module=question['module_name']
        )

if __name__ == "__main__":
    filename = "scripts/lijing-quest.json"
    main(filename)
